/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.streamline.services.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Service class to handle annotation-related operations
 */
public class AnnotationServices {

    /**
     * Select a list of annotated elements from a given array if the parameter "a" is their definer class
     * @param a definer class
     * @param elements source elements 
     * @param <T> generic type of annotation type
     * @return selected annotation elements
     */
    private static <T extends Annotation> Collection<T> process( Class<T> a, AnnotatedElement... elements  ){
        ArrayList<T> list = new ArrayList<T>();
        for( AnnotatedElement element : elements ){
            for( Annotation ae : element.getAnnotations() )
                if( a.isInstance( ae ) )
                    list.add((T) ae);
        }
        return list;
    }

    /**
     * Gets an annotation of a field with the given name defined by the given class
     * @param c definer class
     * @param a class reference of the annotation
     * @param name name of the field
     * @param <T> generic type of the annotation
     * @return annotation instance
     * @throws NoSuchFieldException
     */
    public static <T extends Annotation> T getAnnotation( Class c, Class<T> a, String name) throws NoSuchFieldException {
        return c.getField( name ).getAnnotation( a );
    }

    /**
     * Gets an annotation of a method with the given name defined by the given class
     * @param c definer class
     * @param a class reference of the annotation
     * @param name name of the method
     * @param parameters types of the parameterlist of the method
     * @param <T> generic type of the annotation
     * @return annotation instance
     * @throws NoSuchMethodException
     */
    public static <T extends Annotation> T getAnnotation( Class c, Class<T> a, String name, Class... parameters) throws NoSuchMethodException {
        return c.getMethod( name, parameters ).getAnnotation( a );
    }

    /**
     * Gets all annotations of a field with the given name defined in the given class
     * @param c definer class
     * @param name name of the field
     * @return iterable of the annotation instances
     * @throws NoSuchFieldException
     */
    public static Iterable<Annotation> getAnnotations( Class c, String name) throws NoSuchFieldException {
        return process( Annotation.class, c.getField( name ) );
    }

    /**
     * Gets all annotations of a method with the given name and parameterlist defined in the given class
     * @param c definer class
     * @param name name of the method
     * @param parameters types of the parameterlist of the method
     * @return iterable of the annotation instances
     * @throws NoSuchMethodException
     */
    public static Iterable<Annotation> getAnnotations( Class c, String name, Class... parameters) throws NoSuchMethodException {
        return process( Annotation.class, c.getMethod( name, parameters ) );
    }

    /**
     * With a given annotation types, this method gets a collection of annotation, instances of that type and associated to members (field or method)
     * @param c definer class
     * @param a annotation type
     * @param <T> generic type of the annotation
     * @return iterable of the annotation instances
     */
    public static <T extends Annotation> Iterable<T> getAnnotations( Class c, Class<T> a){
        ArrayList<T> list = new ArrayList<T>();
        list.addAll( process( a, c ) );
        list.addAll( process( a, c.getFields() ) );
        list.addAll( process( a, c.getMethods() ) );
        return list;
    }

}
